<template>
  <div class="video-player">
    <video
      ref="videoRef"
      @timeupdate="onTimeUpdate"
      @loadedmetadata="onLoadedMetadata"
      @ended="onEnded"
      autoplay
      src="//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4"
    ></video>
    <div class="controls">
      <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
      <div class="progress-bar-container">
        <div class="progress-bar" @click="seek">
          <div class="progress" :style="{ width: `${progress}%` }"></div>
          <div class="ticks">
            <div v-for="tick in ticks" :key="tick" class="tick" :style="{ left: `${tick}%` }"></div>
          </div>
        </div>
      </div>
      <span class="time">{{ formatTime(currentTime) }} / {{ formatTime(duration) }}</span>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed,  onUnmounted } from 'vue';

export default defineComponent({
  name: 'VideoPlayerWithTicks',
  props: {
    videoSrc: {
      type: String,
      required: true,
    },
    tickInterval: {
      type: Number,
      default: 10, // 每10秒一个刻度
    },
  },
  setup(props) {
    const videoRef = ref<HTMLVideoElement | null>(null);
    const currentTime = ref(0);
    const duration = ref(0);
    const isPlaying = ref(false);

    const progress = computed(() => {
      return (currentTime.value / duration.value) * 100 || 0;
    });

    const ticks = computed(() => {
      if (duration.value === 0) return [];
      const tickCount = Math.floor(duration.value / props.tickInterval);
      return Array.from({ length: tickCount }, (_, i) => (i + 1) * props.tickInterval * 100 / duration.value);
    });

    const togglePlay = () => {
      if (videoRef.value) {
        if (isPlaying.value) {
          videoRef.value.pause();
        } else {
          videoRef.value.play();
        }
        isPlaying.value = !isPlaying.value;
      }
    };

    const onTimeUpdate = () => {
      if (videoRef.value) {
        currentTime.value = videoRef.value.currentTime;
      }
    };

    const onLoadedMetadata = () => {
      if (videoRef.value) {
        duration.value = videoRef.value.duration;
      }
    };

    const onEnded = () => {
      isPlaying.value = false;
    };

    const seek = (event: MouseEvent) => {
      if (videoRef.value) {
        const progressBar = event.currentTarget as HTMLElement;
        const clickPosition = event.clientX - progressBar.getBoundingClientRect().left;
        const percentClicked = clickPosition / progressBar.offsetWidth;
        videoRef.value.currentTime = percentClicked * duration.value;
      }
    };

    const formatTime = (timeInSeconds: number): string => {
      const minutes = Math.floor(timeInSeconds / 60);
      const seconds = Math.floor(timeInSeconds % 60);
      return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
    };

    onUnmounted(() => {
      if (videoRef.value) {
        videoRef.value.pause();
        videoRef.value.src = '';
        videoRef.value.load();
      }
    });

    return {
      videoRef,
      currentTime,
      duration,
      isPlaying,
      progress,
      ticks,
      togglePlay,
      onTimeUpdate,
      onLoadedMetadata,
      onEnded,
      seek,
      formatTime,
    };
  },
});
</script>

<style scoped>
.video-player {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}

video {
  width: 100%;
  display: block;
}

.controls {
  display: flex;
  align-items: center;
  background-color: #f0f0f0;
  padding: 10px;
}

button {
  background-color: #4CAF50;
  border: none;
  color: white;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin-right: 10px;
  cursor: pointer;
}

.progress-bar-container {
  flex-grow: 1;
  position: relative;
}

.progress-bar {
  height: 10px;
  background-color: #ddd;
  cursor: pointer;
  position: relative;
}

.progress {
  height: 100%;
  background-color: #4CAF50;
  position: absolute;
  top: 0;
  left: 0;
}

.ticks {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.tick {
  position: absolute;
  top: -5px;
  width: 2px;
  height: 20px;
  background-color: #888;
}

.time {
  margin-left: 10px;
  font-size: 14px;
}
</style>